﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLite;
using System.IO;

namespace MOJsdk
{
    public class MovieDataBaseInterop : SQLiteConnection
    {
        public static string DataBaseLocal
        { get { return Path.Combine(MojFoldersEnum.DatabaseFolder.Value(), "Movies"); } }

        public MovieDataBaseInterop()
            : base(DataBaseLocal)
        {
            CreateTable<MoJBasicMovie>();
        }


        public bool MovieExist(MoJBasicMovie Movie)
        {
            return Table<MoJBasicMovie>().Contains(Movie);
        }
        public bool MovieExist(string MovieCode)
        {
            return MovieExist((MoJBasicMovie)MovieCode);
        }

        public void UpdateMovie(MoJBasicMovie Movie)
        {
            if (MovieExist(Movie))
                Update(Movie, typeof(MoJBasicMovie));
            else
                Insert(Movie, typeof(MoJBasicMovie));
        }

        public MoJBasicMovie GetMovie(string MovieCode)
        {
            if (!MovieExist(MovieCode))
                return null;

            return Get<MoJBasicMovie>(MovieCode);
        }
        public bool RemoveMovie(string MovieCode)
        {
            if (!MovieExist(MovieCode))
                return false;

            Delete<MoJBasicMovie>(MovieCode);
            return true;
        }

        public List<MoJBasicMovie> Movies
        {
            get
            {
                return new List<MoJBasicMovie>(Table<MoJBasicMovie>());
            }
        }
    }
    public class FileDataBaseInterop : SQLiteConnection
    {
        public static string DataBaseLocal
        { get { return Path.Combine(MojFoldersEnum.DatabaseFolder.Value(), "Files"); } }

        public FileDataBaseInterop()
            : base(DataBaseLocal)
        { CreateTable<MoJBasicFile>(); }

        public bool FileExist(MoJBasicFile File)
        { return Table<MoJBasicFile>().Contains(File); }
        public bool FileExist(int FileID)
        { return FileExist((MoJBasicFile)FileID); }
        public int PathIndex(string FilePath)
        {
            if (Table<MoJBasicFile>().Count(z => z.FilePath.ToLower() == FilePath.ToLower()) > 0)
                return Table<MoJBasicFile>().First(z => z.FilePath.ToLower() == FilePath.ToLower()).FileID;

            return -1;
        }

        public MoJBasicFile GetFile(int FileId)
        {
            if (!FileExist(FileId))
                return null;

            return Get<MoJBasicFile>(FileId);
        }
        public void UpdateFile(MoJBasicFile File)
        {
            if (FileExist(File))
                Update(File, typeof(MoJBasicFile));
        }
        public int AddNewFile(MoJBasicFile File)
        {
            if (PathIndex(File.FilePath) != -1)
                return -1;

            return Insert(File, typeof(MoJBasicFile));
        }
        public int AddNewFile(string FilePath, int FileIndex, MOJFileTypes FileType, string MovieCode, string LanguageCode = "")
        {
            if (PathIndex(FilePath) != -1)
                return -1;

            MoJBasicFile file = new MoJBasicFile()
            {
                FilePath = FilePath,
                FileIndex = FileIndex,
                FileType = (int)FileType,
                MovieCode = MovieCode,
                OriginalName = System.IO.Path.GetFileNameWithoutExtension(FilePath),
                DiskID = TOOLS.GetDriveID(FilePath),
                Language = LanguageCode,
            };

            return AddNewFile(file);
        }

        public bool RemoveFile(int FileId)
        {
            if (!FileExist(FileId))
                return false;

            Delete<MoJBasicFile>(FileId);
            return true;
        }

        public List<MoJBasicFile> Files
        {
            get
            {
                return new List<MoJBasicFile>(Table<MoJBasicFile>());
            }
        }
    }
}
